package com.caration.shell.workspace2D;

import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import com.caration.shell.R;
import com.caration.shell.cr3d.GLHandler;
import com.caration.shell.item.AppWidgetInfo;
import com.caration.shell.utils.Reflect;

public class ShellAppAppWidgetHostView extends AppWidgetHostView {
	public boolean a = false;
	private boolean mHasPerformedLongPress;
	private CheckForLongPress mPendingCheckForLongPress;
	private LayoutInflater mInflater;

	public ShellAppAppWidgetHostView(Context context) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		setDrawingCacheEnabled(false);
		Reflect.setMotionEventSplittingDisable(this);
	}

	public final void cancelLongPress() {
		super.cancelLongPress();
		mHasPerformedLongPress = false;
		if (mPendingCheckForLongPress != null)
			removeCallbacks(mPendingCheckForLongPress);
	}

	public final void b() {
		this.a = true;
	}

	public final void c() {
		this.a = false;
	}

	// d
	public final void d() {
		((ScreenLayout) this.getParent()).a((ScreenLayout.LayoutParams) this
				.getLayoutParams());
		GLHandler.post(new Runnable() {

			@Override
			public void run() {
				requestLayout();
				((AppWidgetInfo) getTag()).p.a();
			}
		});
	}

	// e
	public final void resize() {
		Log.w("ddddd", "dddddd resize start");
		((ScreenLayout) this.getParent()).b((ScreenLayout.LayoutParams) this
				.getLayoutParams());
	}

	public final void f() {
		destroyDrawingCache();
		mPendingCheckForLongPress = null;
	}

	protected final View getErrorView() {
		return mInflater.inflate(R.layout.appwidget_error, this, false);
	}

	private void postCheckForLongClick() {
		mHasPerformedLongPress = false;

		if (mPendingCheckForLongPress == null) {
			mPendingCheckForLongPress = new CheckForLongPress();
		}
		mPendingCheckForLongPress.rememberWindowAttachCount();
		postDelayed(mPendingCheckForLongPress,
				ViewConfiguration.getLongPressTimeout());
	}

	public final boolean onInterceptTouchEvent(MotionEvent ev) {
		// Consume any touch events for ourselves after longpress is triggered
		if (mHasPerformedLongPress) {
			mHasPerformedLongPress = false;
			return true;
		}

		// Watch for longpress events at this level to make sure
		// users can always pick up this widget
		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN: {
			postCheckForLongClick();
			break;
		}

		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			mHasPerformedLongPress = false;
			if (mPendingCheckForLongPress != null) {
				removeCallbacks(mPendingCheckForLongPress);
			}
			break;
		}

		// Otherwise continue letting touch events fall through to children
		return false;
	}

	class CheckForLongPress implements Runnable {
		private int mOriginalWindowAttachCount;

		public void run() {
			if ((getParent() != null) && hasWindowFocus()
					&& mOriginalWindowAttachCount == getWindowAttachCount()
					&& !mHasPerformedLongPress) {
				if (performLongClick()) {
					mHasPerformedLongPress = true;
				}
			}
		}

		public void rememberWindowAttachCount() {
			mOriginalWindowAttachCount = getWindowAttachCount();
		}
	}
}
